package com.lanju.dao;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.lanju.entity.DepositSlip;

@Repository
public class DepositSlipDao {
	
    @PersistenceContext(unitName = "financialmarketingUnit")
    private EntityManager entityManager;
    
    @Autowired
   	SessionFactory sessionFactory;
    
    public void insertDepositSlip(DepositSlip obj){
    	try {
    		entityManager.persist(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public List<DepositSlip> queryDepositSlip(Map<String,Object> map){
    	try {
			StringBuffer sql =new StringBuffer();
			sql.append(" from DepositSlip where 1=1 ");
			if(!StringUtils.isEmpty(map.get("contractId"))){
				sql.append(" and contractId ='"+map.get("contractId").toString()+"'");
			}
			if(!StringUtils.isEmpty(map.get("userId"))){
				sql.append(" and userId ='"+map.get("userId").toString()+"'");
			}
			sql.append(" order by createTime desc");
			Query query=entityManager.createQuery(sql.toString());
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    }

    public DepositSlip queryDepotSliById(Map<String,Object> map){
    	try {
			StringBuffer sql =new StringBuffer();
			sql.append(" from DepositSlip where slipId ='"+map.get("slipId").toString()+"'");
			Query query=entityManager.createQuery(sql.toString());
			return (DepositSlip) query.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    }
    
    public void delDepotSliById(DepositSlip obj){
    	try {
			entityManager.remove(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public DepositSlip updateDepositSlip(DepositSlip obj){
    	try {
			 return entityManager.merge(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
    }
}
